package com.ysg.utils;

import android.content.Context;
import android.content.SharedPreferences;
import androidx.annotation.NonNull;

import com.alibaba.fastjson.JSON;
import com.ysg.base.BaseApplication;
import com.ysg.http.data.entity.mine.UserConfigEntity;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * SharedPreferences工具类
 */
public final class YSPUtils {
    private static Map<String, YSPUtils> sSPMap = new HashMap<>();
    private SharedPreferences sp;

    /**
     * 获取SP实例
     */
    public static YSPUtils getInstance() {
        return getInstance("");
    }

    /**
     * 获取SP实例
     */
    public static YSPUtils getInstance(String spName) {
        if (isSpace(spName)) spName = "zhiyu";
        YSPUtils sp = sSPMap.get(spName);
        if (sp == null) {
            sp = new YSPUtils(spName);
            sSPMap.put(spName, sp);
        }
        return sp;
    }

    private YSPUtils(final String spName) {
        sp = BaseApplication.getContext().getSharedPreferences(spName, Context.MODE_PRIVATE);
    }

    /**
     * SP中写入String
     */
    public void put(@NonNull final String key, @NonNull final String value) {
        sp.edit().putString(key, value).apply();
    }

    /**
     * SP中读取String
     */
    public String getString(@NonNull final String key) {
        return getString(key, "");
    }

    /**
     * SP中读取String
     */
    public String getString(@NonNull final String key, @NonNull final String defaultValue) {
        return sp.getString(key, defaultValue);
    }

    /**
     * SP中写入int
     */
    public void put(@NonNull final String key, final int value) {
        sp.edit().putInt(key, value).apply();
    }

    /**
     * SP中读取int
     */
    public int getInt(@NonNull final String key) {
        return getInt(key, -1);
    }

    /**
     * SP中读取int
     */
    public int getInt(@NonNull final String key, final int defaultValue) {
        return sp.getInt(key, defaultValue);
    }

    /**
     * SP中写入long
     */
    public void put(@NonNull final String key, final long value) {
        sp.edit().putLong(key, value).apply();
    }

    /**
     * SP中读取long
     */
    public long getLong(@NonNull final String key) {
        return getLong(key, -1L);
    }

    /**
     * SP中读取long
     */
    public long getLong(@NonNull final String key, final long defaultValue) {
        return sp.getLong(key, defaultValue);
    }

    /**
     * SP中写入float
     */
    public void put(@NonNull final String key, final float value) {
        sp.edit().putFloat(key, value).apply();
    }

    /**
     * SP中读取float
     */
    public float getFloat(@NonNull final String key) {
        return getFloat(key, -1f);
    }

    /**
     * SP中读取float
     *
     * @param key 键
     * @param defaultValue 默认值
     * @return 存在返回对应值，不存在返回默认值{@code defaultValue}
     */
    public float getFloat(@NonNull final String key, final float defaultValue) {
        return sp.getFloat(key, defaultValue);
    }

    /**
     * SP中写入boolean
     * @param value 值
     */
    public void put(@NonNull final String key, final boolean value) {
        sp.edit().putBoolean(key, value).apply();
    }

    /**
     * SP中读取boolean
     */
    public boolean getBoolean(@NonNull final String key) {
        return getBoolean(key, false);
    }

    /**
     * SP中读取boolean
     */
    public boolean getBoolean(@NonNull final String key, final boolean defaultValue) {
        return sp.getBoolean(key, defaultValue);
    }

    /**
     * SP中写入String集合
     */
    public void put(@NonNull final String key, @NonNull final Set<String> values) {
        sp.edit().putStringSet(key, values).apply();
    }

    /**
     * SP中读取StringSet
     */
    public Set<String> getStringSet(@NonNull final String key) {
        return getStringSet(key, Collections.<String>emptySet());
    }

    /**
     * SP中读取StringSet
     */
    public Set<String> getStringSet(@NonNull final String key, @NonNull final Set<String> defaultValue) {
        return sp.getStringSet(key, defaultValue);
    }

    /**
     * SP中获取所有键值对
     */
    public Map<String, ?> getAll() {
        return sp.getAll();
    }

    /**
     * SP中是否存在该key
     */
    public boolean contains(@NonNull final String key) {
        return sp.contains(key);
    }

    /**
     * SP中移除该key
     */
    public void remove(@NonNull final String key) {
        sp.edit().remove(key).apply();
    }

    /**
     * SP中清除所有数据
     */
    public void clear() {
        sp.edit().clear().apply();
    }

    private static boolean isSpace(final String s) {
        if (s == null) return true;
        for (int i = 0, len = s.length(); i < len; ++i) {
            if (!Character.isWhitespace(s.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    public void saveToken(String token){
        put("token", token);
    }

    public String getToken(){
        return getString("token");
    }

    public void saveLogin(boolean autoLogin){
        put("autoLogin", autoLogin);
    }

    public boolean getLogin(){
        return getBoolean("autoLogin");
    }

    public void saveUserId(String userId){
        put("userId", userId);
    }

    public String getUserId(){
        return getString("userId");
    }

    public void saveUserName(String userName){
        put("userName", userName);
    }
    public String getUserName(){
        return getString("userName");
    }

    public void saveUserPhone(String userPhone){
        put("userPhone", userPhone);
    }
    public String getUserPhone(){
        return getString("userPhone");
    }

    public void saveTxCode(String txCode){
        put("txCode", txCode);
    }
    public String getTxCode(){
        return getString("txCode");
    }

    public void saveUserCode(String userCode){
        put("userCode", userCode);
    }
    public String getUserCode(){
        return getString("userCode");
    }

    public void saveUserSex(String sex){
        put("userSex", sex);
    }
    public String getUserSex(){
        return getString("userSex");
    }

    public void saveUserPic(String userPic){
        put("userPic", userPic);
    }
    public String getUserPic(){
        return getString("userPic");
    }

    public void saveUserVip(String vip){
        put("userVip", vip);
    }
    public boolean getUserVip(){
        return YStringUtil.eq(1, getString("userVip"));
    }

    public void saveShowVideoAudioPrice(String txUserId, boolean b){
        put("show_video_audio_price_"+txUserId, b);
    }
    public boolean getShowVideoAudioPrice(String txUserId){
        return getBoolean("show_video_audio_price_"+ txUserId, false);
    }
    public void saveOAID(String OAID){
        put("OAID", OAID);
    }

    public String getOAID(){
        return getString("OAID");
    }


    public void saveBaseUrl(String baseUrl){
        put("baseUrl", baseUrl);
    }

    public String getBaseUrl(){
        return getString("baseUrl");
    }

    public void savePrivacyFlag(){
        put("privacy_flag", true);
    }

    public boolean getPrivacyFlag(){
        return getBoolean("privacy_flag", false);
    }

    public void saveUserConfig(UserConfigEntity entity){
        put("userConfig", JSON.toJSONString(entity));
    }
    public UserConfigEntity getUserConfig(){
        UserConfigEntity entity = null;
        String userConfig = getString("userConfig");
        if (YStringUtil.isNotEmpty(userConfig)){
            entity = JSON.parseObject(userConfig, UserConfigEntity.class);
        }
        return entity;
    }

    public void saveTicketFree(){
        put("ticket_"+getUserId(), true);
    }

    public boolean getTicketFree(){
        return getBoolean("ticket_"+getUserId(), false);
    }

    public void saveWhiteIp(boolean isWhite){
        put("is_white_ip", isWhite);
    }

    public boolean isWhiteIp(){
        return getBoolean("is_white_ip", false);
    }

}
